/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import javax.mail.PasswordAuthentication;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import resourses.Constants;

/**
 *
 * @author Yash
 */
public class MailUtility {

    static Properties props = new Properties();

    static {
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
    }

    public static boolean sendMail(String to, String msg, String subject) {

        boolean flag=false;
        Session session;
        session = Session.getInstance(props,
            new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(Constants.mailUsername, Constants.mailPassword);
            }
        });
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(Constants.mailFrom));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(msg);
            Transport.send(message);
            flag=true;


        } catch (MessagingException e) {
            flag=false;
        }
        return flag;
    }
}
